﻿using System;
using System.ComponentModel.DataAnnotations;
using System.Diagnostics.Contracts;

namespace TaskManager.Domain.BusinessObjects
{
    /// <summary>
    /// User comment that belongs to a task
    /// </summary>
    public class Comment 
    {
        /// <summary>
        /// Task comment unique identifier
        /// </summary>
        [Key]
        public int Id { get; set; }

        /// <summary>
        /// Reference to a task
        /// </summary>
        public virtual Task Task { get; set; }

        /// <summary>
        /// Task identifier that is used as foreign key
        /// </summary>
        public int TaskId { get; set; }

        /// <summary>
        /// User comment
        /// </summary>
        public string Message { get; private set; }

        /// <summary>
        /// Date and time of creation
        /// </summary>
        public DateTime Created { get; private set; }

        /// <summary>
        /// User who added a comment
        /// </summary>
        public virtual User User { get; private set; }

        /// <summary>
        /// User identifier that is used as foreign key
        /// </summary>
        public int UserId { get; set; }

        /// <summary>
        /// Parameterless constructor
        /// </summary>
        protected Comment()
        {
        }

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="message">Message</param>
        /// <param name="task">Task</param>
        /// <param name="user">User who wrote a comment</param>
        public Comment(string message, Task task, User user)
        {
            Contract.Requires(!string.IsNullOrEmpty(message));
            Contract.Requires(task != null);
            Contract.Requires(user != null);

            User = user;
            Created = DateTime.Now;
            Message = message;
            Task = task;

            task.AddComment(this);
        }
    }
}